package cn.bdqn.controller;

import cn.bdqn.pojo.Plan;
import cn.bdqn.pojo.SalChance;
import cn.bdqn.pojo.User;
import cn.bdqn.service.PlanService;
import cn.bdqn.service.SalChanceService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/plan")
public class PlanController {
    @Resource
    private SalChanceService chanceService;

    @Resource
    PlanService planService;

    /**
     * 列表展示
     *
     * @param name
     * @param title
     * @param linkMan
     * @param pageIndex
     * @param model     * @return
     */
    @RequestMapping("/list")
    public String list(String name, String title, String linkMan,
                       @RequestParam(defaultValue = "1") Integer pageIndex,
                       Model model, HttpSession session) {
        //设置每页显示的条数
        int pageSize = 3;
        //当前人id
        User user = (User)session.getAttribute("loginUser");
        //查询每页显示的数据
        IPage<SalChance> chancesByPage = chanceService.getChancesByPage(name, title, linkMan, pageIndex, pageSize,user.getUsrId());
        model.addAttribute("chancePager", chancesByPage);
        //条件回显
        //查询条件回显
        model.addAttribute("name", name);
        model.addAttribute("linkMan", linkMan);
        model.addAttribute("title", title);
        return "plan/list";
    }

    /**
     * 跳转新增开发计划页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/add")
    public String add(Long id, Model model) {
        //根据销售机会ID，查询信息
        SalChance chance = chanceService.getChance(id);
        model.addAttribute("chance", chance);
        //根据销售机会ID，查询开发计划
        List<Plan> planList = planService.getPlanByChcId(id);
        model.addAttribute("planList", planList);
        //显示当前时间
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        model.addAttribute("now", dtf.format(LocalDate.now()));
        return "plan/add";
    }

    /**
     * 添加开发计划
     *
     * @param plan
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(Plan plan) {
        Map<String, Object> map = new HashMap<>();
        try {
            planService.addPlan(plan);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }

    /**
     * 修改开发计划
     *
     * @param plan
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> update(Plan plan) {
        Map<String, Object> map = new HashMap<>();
        try {
            planService.updatePlan(plan);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }

    /**
     * 删除开发计划
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map delete(Long id) {
        Map<String, Object> map = new HashMap<>();
        try {
            planService.deletePlan(id);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }

    /**
     * 跳转执行查看计划页面!详情
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/view")
    public String view(Long id, Model model) {
        //根据销售机会ID，查询信息
        SalChance chance = chanceService.getChance(id);
        model.addAttribute("chance", chance);
        //根据销售机会ID，查询开发计划
        List<Plan> planList = planService.getPlanByChcId(id);
        model.addAttribute("planList", planList);
        return "plan/view";
    }

    /**
     * 跳转添加执行结果页面
     *
     * @param id
     * @param model * @return
     */
    @RequestMapping("/addPlanResult")
    public String addPlanResult(Long id, Model model) {
        //根据销售机会ID，查询信息
        SalChance chance = chanceService.getChance(id);
        model.addAttribute("chance", chance);
    //根据销售机会ID，查询开发计划
        List<Plan> planList = planService.getPlanByChcId(id);
        model.addAttribute("planList", planList);
        return "plan/result";
    }

    /**
     * 保存执行效果
     * @param plan
     * @return
     */
    @RequestMapping("/savePlanResult")
    @ResponseBody
    public Map<String, Object> savePlanResult(Plan plan) {
        Map<String, Object> map = new HashMap<>();
        try {
            planService.updatePlan(plan);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }

    /**
     * 处理销售机会，开发成功操作
     * @param id
     * @return
     */
    @RequestMapping("/success")
    @ResponseBody
    public Map success(Long id) {
        Map<String, Object> map = new HashMap<>();
        try {
            chanceService.successChance(id);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }

    /**
     * 处理销售机会，开发失败操作
     * @param id
     * @return
     */
    @RequestMapping("/fail")
    @ResponseBody
    public Map fail(Long id) {
        Map<String, Object> map = new HashMap<>();
        try {
            chanceService.failChance(id);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }
}
